
import { LGXEnums } from '../../common/sysenum.js'
var fillTypeEnums = LGXEnums.LGXFillStyle;
import linegradientutil from './linegradientutil.js'
import radialgradientutil from './radialgradientutil.js';

import UtilFuncs from "../../common/tools/utilfuncs";
var funcs = UtilFuncs.UtilFuncs;

import { CoordTRFUtil } from '../../common/tools/coordtool.js';

var FillUtil = {}

FillUtil.active = function (fillDef, map, graph, mapinfo, isPaintSybolUnitFlag, transformInfo) {
    var grd = null;
    if (fillDef.fillStyle == fillTypeEnums.EMPTY) {
        grd = null;
    }
    else if (fillDef.fillStyle == fillTypeEnums.SOLID) {
        let tmpColor = fillDef.fillColor.getCloneCopy();
        if (graph.getSelect()) {
            tmpColor.alpha = Math.ceil(tmpColor.alpha / 2);
        }
        grd = tmpColor.toHexString();
    }
    else if (fillDef.fillStyle == fillTypeEnums.radialGradient_complex) {
        let util = radialgradientutil.RadialGradientUtil;
        var poslist = util.getGradientCoord(fillDef.dir, graph, mapinfo, isPaintSybolUnitFlag);
        if (poslist) {
            let p1 = poslist[0];
            let p2 = poslist[1];
            let radius = funcs.distancePoint(p1, p2, 1);
            grd = map.createRadialGradient(p1.x, p1.y, 0, p1.x, p1.y, radius);
            for (let i = 0; i < fillDef.stopColorArr.length; i++) {
                let t = fillDef.stopColorArr[i];
                grd.addColorStop(t.stop, t.color.toHexString());
            }
        }
    }
    else if (fillDef.gradientInfo) {
        let fillGradientInfo = fillDef.gradientInfo;

        let mtx = graph.getLocalMatrix();
        let localP1 = fillGradientInfo.stPt;
        let localP2 = fillGradientInfo.endPt;

        let worldPt1 = mtx.MultT({ x: localP1.x, y: localP1.y, z: 0 });
        let worldPt2 = mtx.MultT({ x: localP2.x, y: localP2.y, z: 0 });

        let p1 = worldPt1;
        let p2 = worldPt2;
        if (p1 && p2) {
            let rotAngle = funcs.degree2Radian(fillGradientInfo.degree);
            let midPt = funcs.getMidPoint(p1, p2);
            let rotp1 = funcs.rotate(p1, midPt, rotAngle);
            let rotp2 = funcs.rotate(p2, midPt, rotAngle);
            let sp1 = null;
            let sp2 = null;
            let errorFlag = false;
            if (isPaintSybolUnitFlag) {
                let wpt1 = CoordTRFUtil.transLocalPt2World(rotp1, 0, transformInfo);
                let wpt2 = CoordTRFUtil.transLocalPt2World(rotp2, 0, transformInfo);
                sp1 = CoordTRFUtil.world2Screen(wpt1, mapinfo);
                sp2 = CoordTRFUtil.world2Screen(wpt2, mapinfo);
            }
            else {
                sp1 = CoordTRFUtil.world2Screen(rotp1, mapinfo);
                sp2 = CoordTRFUtil.world2Screen(rotp2, mapinfo);
            }
            try {
                grd = map.createLinearGradient(sp1.x, sp1.y, sp2.x, sp2.y);
                for (let i = 0; i < fillGradientInfo.stopColorArr.length; i++) {
                    let t = fillGradientInfo.stopColorArr[i];
                    let tmpColor = t.color.getCloneCopy();
                    if (graph.getSelect()) {
                        tmpColor.alpha = Math.ceil(tmpColor.alpha / 2);
                    }
                    grd.addColorStop(t.stop, tmpColor.toHexString());
                }
            }
            catch (e) {
                errorFlag = true;
                console.log(e);
            }
            finally {
                if (errorFlag){
                    grd = null;
                }
            }
        }
    }

    if (grd != null) {
        map.fillStyle = grd;
        map.fill();
    }
}



export { FillUtil }
